package com.kayt.cyberBar.cuser.business.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kayt.cyberBar.common.PageData;
import com.kayt.cyberBar.common.dao.EntityDao;
import com.kayt.cyberBar.common.utils.TimeUtils;
import com.kayt.cyberBar.cuser.business.dao.IStoreIndexPictureDao;
import com.kayt.cyberBar.cuser.business.entity.po.BStoreIndexPicture;
import com.kayt.cyberBar.cuser.business.entity.req.vo.StoreIndexPictureReqVO;

@Service("storeIndexPictureDao")
public class StoreIndexPictureDaoImpl implements IStoreIndexPictureDao {
	@Autowired
	private EntityDao entityDao;

	@Override
	public void saveOrUpdate(StoreIndexPictureReqVO vo) throws SQLException {
		if (vo.getId() != null) {
			BStoreIndexPicture storeIndexPicture = entityDao.findById(Long.parseLong(vo.getId()), BStoreIndexPicture.class);
			storeIndexPicture.setUpdateTime(TimeUtils.generateTimeStr());
			storeIndexPicture.setPicUrl(vo.getPicUrl());
			storeIndexPicture.setSeq(vo.getSeq() );
			storeIndexPicture.setPageUrl(vo.getPageUrl());
			storeIndexPicture.setStoreCode("0000");
			storeIndexPicture.setStatus(vo.getStatus());
			entityDao.update(storeIndexPicture);
		} else {
			BStoreIndexPicture storeIndexPicture = new BStoreIndexPicture();
			storeIndexPicture.setCreateTime(TimeUtils.generateTimeStr());
			storeIndexPicture.setUpdateTime(TimeUtils.generateTimeStr());
			storeIndexPicture.setPicUrl(vo.getPicUrl());
			storeIndexPicture.setSeq(vo.getSeq() );
			storeIndexPicture.setPageUrl(vo.getPageUrl());
			storeIndexPicture.setStoreCode("0000");
			storeIndexPicture.setStatus(vo.getStatus());
			entityDao.save(storeIndexPicture);
		}

	}

	@Override
	public PageData<BStoreIndexPicture> getIndexPictureList(StoreIndexPictureReqVO vo){
		String whereJpql = "1 = 1";
		List<Object> queryParams = new ArrayList<>();
		if(StringUtils.isNotEmpty("0000")){
			whereJpql += " and o.storeCode = ? ";
			queryParams.add("0000");
		}
		if(StringUtils.isNotEmpty(vo.getStatus())){
			whereJpql += " and status = ? ";
			queryParams.add(vo.getStatus());
		}
		LinkedHashMap<String,String> orderBy = new LinkedHashMap<>();
		if(StringUtils.isNotEmpty(vo.getSortOrder()) && StringUtils.isNotEmpty(vo.getSortName())){
			orderBy.put(vo.getSortName(), vo.getSortOrder());
		}
		PageData<BStoreIndexPicture> pageData;
		try {
			pageData = entityDao.getPagingData(BStoreIndexPicture.class, vo.getPageNum(), vo.getPageSize(), whereJpql, queryParams.toArray(), orderBy);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
		return pageData;
	}

}
